package com.lab.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;

/**
 * Created by MiJack on 2015/12/2.
 */
public class WordCountView extends FrameLayout {
    private int normalColor;
    private int fullColor;
    private int maxWordNumber;
    private int currentWordNumber;
    private EditText editView;
    private TextView countView;
    private String hint;

    public WordCountView(Context context) {
        this(context, null);
    }

    public WordCountView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WordCountView(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.WordCountView, defStyleAttr, R.style.WordCountView_Default);
        maxWordNumber = a.getInt(R.styleable.WordCountView_maxLength, 10);
        fullColor = a.getColor(R.styleable.WordCountView_fullColor, ResourceUtil.getThemeColor());
        normalColor = a.getColor(R.styleable.WordCountView_normalColor, getResources().getColor(R.color.ganshi_ded8d7));
        hint = a.getString(R.styleable.WordCountView_textHint);
        a.recycle();
        //加载布局
        View view = LayoutInflater.from(context).inflate(R.layout.base_word_count_view, this, false);
        addView(view);
        editView = (EditText) view.findViewById(R.id.editView);
        editView.setHint(hint);
        countView = (TextView) view.findViewById(R.id.countView);
        countView.setText("0/"+maxWordNumber);
        editView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > maxWordNumber) {
                    s.delete(maxWordNumber, s.length());
                }
                countView.setText(s.length() + "/" + maxWordNumber);
                countView.setTextColor(s.length() == maxWordNumber ? fullColor : normalColor);
            }
        });
    }

    public int getMaxWordNumber() {
        return maxWordNumber;
    }

    public void setMaxWordNumber(int maxWordNumber) {
        if (maxWordNumber<getContent().length()){
            return;
        }
        this.maxWordNumber = maxWordNumber;
    }

    public int getCurrentWordNumber() {
        return currentWordNumber;
    }

    private void setCurrentWordNumber(int currentWordNumber) {
        this.currentWordNumber = currentWordNumber;
    }

    public EditText getEditView() {
        return editView;
    }

    public TextView getCountView() {
        return countView;
    }

    public String getContent() {
        return editView.getText().toString().toString();
    }

    public void setHint(String hint) {
        this.hint = hint;
        editView.setHint(hint);
    }

    public String getHint() {
        return hint;
    }

    public void setText(String text) {
        editView.setText(text);
        if (!TextUtils.isEmpty(text)){
            editView.setSelection(text.length());
        }
    }
}
